#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtSerialPort/QSerialPort>
#include <QString>
#include <QtSerialPort/QSerialPortInfo>
#include <QMessageBox>
#include <QTimer>
#include <QPainter>
#include <QCollator>
#include <QFile>
#include <QFileInfo>
#include <QMutex>
#include "worker.h"
#include <QThread>
#include "sendcountupdater.h"
#include <QSqlDatabase>
#include <QSqlQuery>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

signals:
    void startThread();
    void serialDataSend(const QByteArray data);
    void updateProgress(int ProgressBarValue);
    void sendDataRequest(QByteArray data);
    void requestSendFile(QString filepath,int FrameLen,int FrameGap);
    void requestCancelFile();
    void requestOpenPort(QString portName, int baudRate, int dataBits, int stopBits, int parity);

private slots:
    void handleError(const QString &error);
    void handlePortOpened(bool success, const QString &message);

    void handleUpdateLabel(const QString& text);
    void handleResults(QString result);

    void findSerial();
    //void handleSerialError(QSerialPort::SerialPortError error);
    void on_openBt_clicked();
    void on_sendBt_clicked();
    void on_clearBt_clicked();
    void on_btnClearSend_clicked();
    void on_chkTimSend_stateChanged(int arg1);
    void on_btnSerialCheck_clicked();
    void on_selectFileButton_clicked();
    // void handleFileEncodingAndRead(QFile &file, const QFileInfo &fileinfo);
    // void handleFileTypeAndRead(const QFileInfo &fileinfo);
    // void displayFileInfoAndContent();
    void on_SaveData_Button_clicked();

    //void SendFile();
    void onDataReceivedFromWorker(const QByteArray &result);

    void updateFileProgress(int percent);
    void onFileTransferFinished();


private:
    QString m_currentFilePath;
    bool m_isSendingFile;
    Ui::MainWindow *ui;
    QThread* workerThread;
    worker* worker;
    SendCountUpdater* sendcount;
    QAtomicInt m_sendCount;
    QAtomicInt m_recvCount;
    bool validateHex(const QString &input);
    bool sendEmptyPrompted;
    QByteArray array;
    QMutex sendMutex;
    QMutex serialMutex;
    bool isSendFile = false;
    int FrameCount = 0;
    int ProgressBarValue = 0;
    int FrameLen = 0;
    int FrameGap = 0;
    int FrameNumber = 0;
    int LastFrameLen = 0;
    int ProgressBarStep = 0;
    QString FileText;
    QTimer *FileSendTimer;
    //bool naturalCompare(const QString &s1, const QString &s2);
    QStringList portStringList;
    QAtomicInt sendNum, recvNum;
    QLabel *lblSendNum;
    QLabel *lblRecvNum;
    QLabel *lblPortState;
    void setNumOnLabel(QLabel *lbl, QString strS, long num);
    QTimer *timSend;
    QTimer *timSerial;

    // 新增封装函数声明
    void setupSerialPort();
    QSerialPort::BaudRate getBaudRate(const QString& text);
    QSerialPort::DataBits getDataBits(const QString& text);
    QSerialPort::StopBits getStopBits(const QString& text);
    QSerialPort::Parity getCheckBits(const QString& text);
    QString formatReceivedData(const QByteArray& result);
    QByteArray formatSendData(const QString& input);
    void updateSendCount(int bytesWritten);
    void File_TimerSend();
    void onSendBytesWritten(int bytesWritten);
    bool naturalCompare(const QString &s1,const QString &s2)
    {

        QCollator collator;
        collator.setNumericMode(true);
        return collator.compare(s1,s2) < 0;

    }
};
#endif // MAINWINDOW_H
